package dao;

import entity.Brand;
import utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandDao {

    public List<Brand> selectBrand() {
        Connection connection = null;
        List<Brand> list = new ArrayList<>();
        try {
            connection = DbUtils.getConnection();
            String sql = "select * from tb_brand";
            ResultSet resultSet = connection.prepareStatement(sql).executeQuery();

            while (resultSet.next()) {
                Brand brand = new Brand();
                brand.setId(resultSet.getInt("id"));
                brand.setBrandName(resultSet.getString("brand_name"));
                brand.setDescription(resultSet.getString("description"));
                brand.setOrdered(resultSet.getInt("ordered"));
                brand.setCompanyName(resultSet.getString("company_name"));
                brand.setStatus(resultSet.getInt("status"));
                list.add(brand);
            }

            resultSet.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.closeConn(connection);
        }
        return list;
    }

    public boolean insert(Brand b) {
        Connection connection = null;
        int affectRow = 0;
        try {
            connection = DbUtils.getConnection();

            connection.setAutoCommit(false);
            String sql = "insert into tb_brand(brand_name,company_name,ordered,description)" +
                    " values(?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, b.getBrandName());
            preparedStatement.setString(2, b.getCompanyName());
            preparedStatement.setInt(3, b.getOrdered());
            preparedStatement.setString(4, b.getDescription());

            affectRow += preparedStatement.executeUpdate();
            connection.commit();
            preparedStatement.close();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            DbUtils.closeConn(connection);
        }
        return affectRow >= 1;
    }

    public boolean update(Brand b) {
        Connection connection = null;
        int affectRow = 0;
        try {
            connection = DbUtils.getConnection();

            connection.setAutoCommit(false);


            String sql = "UPDATE `shixun01`.`tb_brand` SET `brand_name` = ?, `company_name` = ?, `ordered` = ?, `description` = ?, `status` = ? WHERE `id` = ?;";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, b.getBrandName());
            preparedStatement.setString(2, b.getCompanyName());
            preparedStatement.setInt(3, b.getOrdered());
            preparedStatement.setString(4, b.getDescription());
            preparedStatement.setInt(5, b.getStatus());
            preparedStatement.setInt(6, b.getId());

            affectRow = preparedStatement.executeUpdate();
            connection.commit();
            preparedStatement.close();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            DbUtils.closeConn(connection);
        }
        return affectRow >= 1;
    }


    public static boolean DeleteByBrandId(Integer id) {
        Connection connection = null;
        int affectRow = 0;
        try {
            connection = DbUtils.getConnection();

            connection.setAutoCommit(false);


            String sql = "delete  from `tb_brand` where id = ?;";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);

            affectRow = preparedStatement.executeUpdate();
            connection.commit();
            preparedStatement.close();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            DbUtils.closeConn(connection);
        }
        return affectRow >= 1;
    }


}
